import functools
import json

from peewee import DatabaseProxy, AutoField, CharField, IntegerField, Model
from playhouse.sqlite_ext import JSONField

db_proxy = DatabaseProxy()


class Base(Model):
    class Meta:
        database = db_proxy


class Source(Base):
    serial = AutoField(primary_key=True)
    path = CharField(max_length=2000)
    name = CharField()
    fingerprint = CharField(null=False, unique=True)


class Sample(Base):
    serial = AutoField(primary_key=True)
    source = CharField(max_length=2000, null=False)
    source_fingerprint = CharField(null=False)
    target_fingerprint = CharField(null=False, unique=True)
    crop_x = IntegerField()
    crop_y = IntegerField()
    crop_width = IntegerField()
    crop_height = IntegerField()
    categories = JSONField(json_dumps=functools.partial(json.dumps, ensure_ascii=False))
